/*
 Copyright (c) 2010 Yann PENIGUEL <yann.peniguel@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

package pong;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author yann
 */
public class PlayingPanel extends JPanel implements KeyListener {

    private int posP1 = 0, posP2 = 0, scoreJ1 = 0, scoreJ2 = 0;
    private int xBall = 0, yBall = 0, xSpeed = 12, ySpeed = 8;


    private void setPosP1(final int pos) {
        if(pos > 0 && pos < getHeight() - 80) posP1 = pos;
    }

    private void setPosP2(final int pos) {
        if(pos > 0 && pos < getHeight() - 80) posP2 = pos;
    }

    private void setPosBall(final int x, final int y) {
        xBall = x;
        yBall = y;
    }

    public final void j2IA() {
        // move P2 down if ball is lower
        if(yBall > posP2 + 60) posP2 += 6;
        // move P2 up if ball is upper
        else if(yBall < posP2 +20) posP2 -= 6;
    }

    public final void nextStep() {

        // the ball is moving
        xBall += xSpeed;
        yBall += ySpeed;

        // ball reached the left side...
        if(xBall < 20) {
            // is the ball out?
            if(yBall + 20 < posP1 || yBall > posP1 +80) {
                System.out.println("point pour J2");
                scoreJ2++;
                 initializePos();
                 return;
            }
            // the ball bounce on P1
            else {
                // replacing the ball
                xBall = Math.abs(20 - xBall);
                // inverting ball move
                xSpeed = -xSpeed;
                // setting ySpeed depending to where P1 touched it
                ySpeed = (yBall - posP1 - 30) / 4;
            }
        }
        // ball reached the right side...
        else if(xBall > getWidth() - 20) {
            // is the ball out?
            if(yBall + 20 < posP2 || yBall > posP2 +80) {
                System.out.println("point pour J1");
                scoreJ1++;
                initializePos();
                return;
            }
            // the ball bounce on P2
            else {
                // replacing the ball
                xBall -=  20;
                // inverting ball move
                xSpeed = -xSpeed;
                // setting ySpeed depending to where P1 touched it
                ySpeed = (yBall - posP2 - 30) / 4;
            }
        }
        // the ball bounce on the top
        if(yBall < 0) {
            yBall = - yBall;
            ySpeed = -ySpeed;
        }
        // the ball bounce on the bottom
        else if (yBall > getHeight() - 20) {
            yBall -= 20;
            ySpeed = - ySpeed;
        }
    }

    public final void initializePos() {
        posP1 = (getHeight() / 2) - 40;
        posP2 = (getHeight() / 2) - 40;
        xBall = getWidth() / 2;
        yBall = getHeight() / 2;
        if(xSpeed < 0) xSpeed = - xSpeed;
        ySpeed = 0;
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(PlayingPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PlayingPanel() {
        this.setFocusable(true);
        this.requestFocus();
        this.addKeyListener(this);

    }

    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);
       g.fillRect(10, posP1, 20, 80); // first player
       g.fillRect( getWidth() - 30, posP2, 20, 80); // second player
       g.fillRect(xBall, yBall , 20, 20); // ball
       String scoreString = "" + scoreJ1 + " - " + scoreJ2; // score string
       g.drawChars( scoreString.toCharArray(), 0, scoreString.length(),
               (getWidth() / 2) - 20, 20); // write the score
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        // moving P1 down?
        if(key == KeyEvent.VK_DOWN) {
            setPosP1(posP1 + 20);
        }
        // moving P1 up?
        else if ( key == KeyEvent.VK_UP )
            setPosP1(posP1 - 20);
    }

    public void keyReleased(KeyEvent e) {

    }


}
